//
//  ShopTableView.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/29.
//

#import "ShopTableView.h"
#import "MineShopTableViewCell.h"

@interface ShopTableView ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ShopTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.backgroundColor = [UIColor clearColor];
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.showsVerticalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineShopTableViewCell"];
    if (!cell) {
        cell = [[MineShopTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MineShopTableViewCell"];
    }
    ShopModel *model = self.modelArr[indexPath.row];
    model.isSelf = self.isMyself;
    cell.model = model;
    
    @weakify(self)
    [cell setEditBlock:^(ShopModel * _Nonnull model) {
     @strongify(self)
        if (self.editBlock) {
            self.editBlock(model);
        }
    }];
    [cell setTopDownBlock:^(ShopModel * _Nonnull model) {
       @strongify(self)
        if (self.topDownBlock) {
            self.topDownBlock(model);
        }
    }];
    [cell setTopBlock:^(ShopModel * _Nonnull model) {
       @strongify(self)
        if (self.topBlock) {
            self.topBlock(model);
        }
    }];
    [cell setDeleteBlock:^(ShopModel * _Nonnull model) {
       @strongify(self)
        if (self.deleteBlock) {
            self.deleteBlock(model);
        }
    }];
    [cell setShouqingBlock:^(ShopModel * _Nonnull model) {
        @strongify(self)
        if (self.shouqingBlock) {
            self.shouqingBlock(model);
        }
    }];
    [cell setUpdateBlock:^(ShopModel * _Nonnull model) {
        @strongify(self)
        if (self.updateBlock) {
            self.updateBlock(model);
        }
    }];
    [cell setGoodsQrcodeBlock:^(ShopModel * _Nonnull model) {
        @strongify(self)
        if (self.goodsQrcodeBlock) {
            self.goodsQrcodeBlock(model);
        }
    }];
    [cell setTransBlock:^(ShopModel * _Nonnull model) {
        @strongify(self)
        if (self.transBlock) {
            self.transBlock(model);
        }
    }];
    [cell setEditTransBlock:^(ShopModel * _Nonnull model) {
        @strongify(self)
        if (self.editTransBlock) {
            self.editTransBlock(model);
        }
    }];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.subject sendNext:indexPath];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.deselectSubject sendNext:indexPath];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}


- (RACSubject *)subject {
    if (!_subject) {
        _subject = [RACSubject subject];
    }
    return _subject;
}
- (RACSubject *)deselectSubject {
    if (!_deselectSubject) {
        _deselectSubject = [RACSubject subject];
    }
    return _deselectSubject;
}

@end
